package com.example.day07_tab;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;


public class BlankFragment extends Fragment {

    private RadioGroup rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private ViewPager fgVp;

    List<Fragment> list = new ArrayList<>();
    MyFragAdapter myFragAdapter;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        initView(view);


        list.add(new BlankFragment4());
        list.add(new BlankFragment5());

        myFragAdapter = new MyFragAdapter(getChildFragmentManager(),list);
        fgVp.setAdapter(myFragAdapter);
        //上下联动
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        fgVp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        fgVp.setCurrentItem(1);
                        break;
                }
            }
        });
        fgVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rg.check(R.id.rb1);
                        break;
                    case 1:
                        rg.check(R.id.rb2);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        return view;
    }

    private void initView(View view) {
        rg = (RadioGroup) view.findViewById(R.id.rg);
        rb1 = (RadioButton) view.findViewById(R.id.rb1);
        rb2 = (RadioButton) view.findViewById(R.id.rb2);
        fgVp = (ViewPager) view.findViewById(R.id.fg_vp);
    }
}